import Vue from 'vue'
import VueRouter from 'vue-router'

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location){
  return originalPush.call(this,location).catch(err => {})
};
Vue.use(VueRouter)

const routes = [
  {
    path: '/sciencesDetail',
    name: 'sciencesDetail',
    component: () => import('../views/SciencesDetail.vue'),
    meta:{
      title: '西湖掌上游 - 详情页面',
      showTabbar: true
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue'),
    meta: {
      title: '西湖登录页面',
      showTabbar: true
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/Register.vue'),
    meta: {
      title: '西湖注册页面',
      showTabbar: true
    }
  },
  {
    path: '/tour',
    name: 'tour',
    meta:{
      activeNum:1,
      title:'西湖掌上游',
      showTabbar: true
      
    },
    component: () => import('../views/Tour.vue'),
  },
  {
    path: '/estimate',
    name: 'estimate',
    meta:{
      activeNum:2,
      title:'西湖掌上游'
    
    },
    component: () => import('../views/Estimate.vue'),
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/User.vue'),
    meta: {
      activeNum:3,
      title: '西湖掌上游 - 用户中心',
    }
  },
  {
    path: '/home/searchPopup',
    name: 'SearchPopup',
    meta:{
      title: '西湖掌上游',
    },
    component: () => import('../views/SearchPopup.vue'),
  },
  {
    path: '/sciencesDetail/Cart',
    name: 'cart',
    meta:{
      title: '西湖掌上游',
    },
    component: () => import('../views/Cart.vue'),
  },
   // 购票成功页面路由
   {
    path: "/sciencesDetail/cart/success",
    name: "success",
    meta:{
      title: '西湖掌上游',
    },
    component: () => import("../views/Success.vue"),
  },
  {
    path: '/sciencesDetail/order',
    name: 'order',
    meta:{
      title: '西湖掌上游',
      showTabbar: true
    },
    component: () => import('../views/Order.vue'),
  },
  {
    path: '/',
    name: 'Home',
    meta:{
      activeNum:0,
    },
    meta:{
      title: '西湖掌上游',
    },
    component: () => import('../views/Home.vue'),
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  next()
})

// 路由拦截,(前置)路由守卫
router.beforeEach((to,from,next)=>{
  // console.log("to",to);
  // console.log("from",from);
  // let token = localStorage.getItem('token')
  // if(to.path=="/estimate"){
  //   // 判断有没有登录,判断token是否存在
  //   if (token) {
  //     next()
  //   }else{
  //     // 表示没有登录
  //     Vue.prototype.$toast("请先登录")
  //     setTimeout(()=>{
  //       next("/user") //跳转到user路由
  //     },1000)
  //   }
  //   return
  // }
  next();//可以顺利要通过的路由,不写next 就不能跳转对应路由
})

export default router
